package com.hualan.sfzy.controller.course;

import com.hualan.sfzy.entity.CourseVideo;
import com.hualan.sfzy.service.course.CourseVideoService;
import com.hualan.sfzy.utils.Result;
import com.hualan.sfzy.vo.CourseVideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courseVideo")
public class CourseVideoController {
    @Autowired
    private CourseVideoService courseVideoService;
    @PostMapping
    public Result add(@RequestBody CourseVideoVo courseVideoVo){
        System.out.println(courseVideoVo);
        int result = courseVideoService.add(courseVideoVo);
        if(result>0)
            return Result.success(result);
        return Result.error("添加失败");
    }
    @GetMapping
    public Result find(){
        List<CourseVideo> courseVideos = courseVideoService.find();
        if(courseVideos!=null)
            return Result.success(courseVideos);
        return Result.error("查询失败");
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        CourseVideo courseVideo = courseVideoService.findById(id);
        if(courseVideo!=null)
            return Result.success(courseVideo);
        return Result.error("查询失败");
    }
    @PostMapping("/page")
    public Result findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody CourseVideoVo courseVideoVo){
        System.out.println(courseVideoVo);
        return Result.success(courseVideoService.findPage(pageNum, pageSize, courseVideoVo));
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        int result = courseVideoService.remove(id);
        if(result>0)
            return Result.success(result);
        return Result.error("删除失败");
    }
    @DeleteMapping("/batch")
    public Result deleteBatch(Integer[] ids){
        int result = courseVideoService.removeBatch(ids);
        if(result>0)
            return Result.success(result);
        return Result.error("删除失败");
    }
    @PutMapping("/newCourse")
    public Result update(@RequestBody CourseVideo courseVideo){
        int result = courseVideoService.edit(courseVideo);
        if(result>0)
            return Result.success(result);
        return Result.error("更新失败");
    }
    @PutMapping
    public Result updateVo(@RequestBody CourseVideoVo courseVideoVo){
        System.out.println(courseVideoVo);
        int result = courseVideoService.edit(courseVideoVo);
        if(result>0)
            return Result.success(result);
        return Result.error("更新失败");
    }

}
